﻿@using System.Globalization
@using Abp.Extensions
@using Abp.MultiTenancy
@using AbpZeroTemplate.Identity
@using Microsoft.AspNetCore.Identity
@model AbpZeroTemplate.Web.Models.Account.LoginFormViewModel
@inject SignInManager SignInManager
@section Scripts
{
    <script abp-src="/view-resources/Views/Account/Login.js" asp-append-version="true"></script>
}

@if (!Model.SuccessMessage.IsNullOrEmpty())
{
    <div class="alert alert-success">
        <button class="close" data-close="alert"></button>
        <span>
            @Model.SuccessMessage
        </span>
    </div>
}

<div class="m-login__signin">
    <div class="m-login__head">
        <h3 class="m-login__title">
            @L("LogIn")
        </h3>
    </div>
    <form class="m-login__form m-form login-form" asp-action="Login" method="post">
        <input type="hidden" name="returnUrl" value="@ViewBag.ReturnUrl" />
        <input type="hidden" name="returnUrlHash" />
        <input type="hidden" name="ss" value="@ViewBag.SingleSignIn" />
        <div class="form-group m-form__group">
            <input class="form-control m-input" type="text" placeholder="@L("UserNameOrEmail")" name="usernameOrEmailAddress" autocomplete="off" value="@(Model.UserNameOrEmailAddress ?? "")" required>
        </div>
        <div class="form-group m-form__group">
            <input class="form-control m-input m-login__form-input--last" type="password" placeholder="@L("Password")" name="password" autocomplete="off" >
        </div>
        <div class="row m-login__form-sub">
            <div class="col m--align-left">
                <label class="m-checkbox m-checkbox--primary">
                    <input type="checkbox" name="rememberMe" value="true">
                    @L("RememberMe")
                    <span></span>
                </label>
            </div>
            <div class="col m--align-right">
                <a href="@Url.Action("ForgotPassword", "Account")" id="forget-password" class="m-link forget-password">@L("ForgotPassword")</a>
            </div>
        </div>
        <div class="m-login__form-action">
            <button type="submit" class="btn btn-primary m-btn m-btn--pill m-btn--custom m-btn--air">@L("LogIn")</button>
        </div>
    </form>
</div>

@{
    var loginProviders = (await SignInManager.GetExternalAuthenticationSchemesAsync())
        .Where(s => !s.DisplayName.IsNullOrWhiteSpace())
        .ToList();

    Func<string, string> getSocialIconClass = (provider) =>
    {
        provider = provider.ToLower(CultureInfo.InvariantCulture);

        return provider;
    };
}

@if ((AbpSession.MultiTenancySide != MultiTenancySides.Host || !ViewBag.IsMultiTenancyEnabled) && loginProviders.Count > 0)
{
    <div class="login-options">
        <h4>@L("LoginWith")</h4>
        <ul class="social-icons">
            <form asp-action="ExternalLogin" method="post">
                <input type="hidden" name="returnUrl" value="@ViewBag.ReturnUrl" />
                <input type="hidden" name="provider" value="" />
                <input type="hidden" name="ss" value="@ViewBag.SingleSignIn" />

                @foreach (var p in loginProviders)
                {
                    <li>
                        <a class="social-login-icon social-icon-color @getSocialIconClass(p.DisplayName)"
                           title="@p.DisplayName"
                           data-provider="@p.DisplayName"
                           href="javascript:;"></a>
                    </li>
                }
            </form>
        </ul>
    </div>
}

<div class="m-stack__item m-stack__item--center">
    <div class="m-login__account">
        @if (Model.IsSelfRegistrationEnabled)
        {
            <a href="@Url.Action("Register", "Account", new { ss = ViewBag.SingleSignIn, returnUrl = ViewBag.ReturnUrl })" id="register-btn" class="m-link m-link--primary m-login__account-link">@L("CreateAnAccount")</a>
            <span class="pipe-divider"> | </span>
        }
        @if (Model.IsTenantSelfRegistrationEnabled)
        {
            <a href="@Url.Action("SelectEdition", "TenantRegistration")" id="register-btn" class="m-link m-link--primary m-login__account-link">@L("NewTenant")</a>
            <span class="pipe-divider"> | </span>
        }
        <a href="@Url.Action("EmailActivation", "Account")" id="email-activation-btn" class="m-link m-link--primary m-login__account-link">@L("EmailActivation")</a>
    </div>
</div>